home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Mac⁄gnuucp 6.14 / source / sysdep.c 9⁄6⁄93 < prev    next >
Encoding:
Text File  |  1993-09-06  |  22.4 KB  |  1,115 lines  |  [TEXT/KAHL]

  1. /*
  2.  * @(#)sysdep.mac 1.2 87/09/23    Copyright 1987 Free Software Foundation, Inc.
  3.  *
  4.  * Copying and use of this program are controlled by the terms of the
  5.  * GNU Emacs General Public License.
  6.  */
  7.  
  8. #ifndef lint
  9. char sysdep_version[] = "@(#)sysdep.mac gnuucp Version Fort Pond Research-6.12";
  10. #endif
  11.  
  12. /*
  13.  * Created 22 September 1987 by John Gilmore, 
  14.  * using code from sun!seismo!psc!jsl (John Labovitz).
  15.  * His comments:
  16.  
  17.     1. Logging cleanup.  It makes it somewhat easier to make the
  18.        right time and date for whatever OS you're using.  I made it
  19.        more modular.
  20.     2. Changes in the login routine to let it be more like real Unix
  21.        systems.  I made a special routine that reads a line from the
  22.        input port, and *then* checks it, instead of only checking each
  23.        character as it comes in.  Also, it handles ^D, which my Unix
  24.        systems uses to make sure there's a prompt.  Doesn't handle break.
  25.  
  26. This needs some more work on the Mac side, like letting the user abort
  27. uuslave.  Right now you have to reset the machine!
  28.  
  29. Hope this is useful.  I'm really interesting in running this thing, although
  30. I'm not quite sure how I'm going to configure it (like how to read mail on
  31. the Mac).  But soon I'll be "potomac!sly!jsl".  What I might do is actually
  32. run this as a server on a PC, connected to the Unix machine via RS232, then
  33. write a program on the Mac that can access the PC via Appletalk (aon the
  34. MacBridge card) and get the mail.  That way, any of our 30 macs can get mail
  35. without being connected to Unix... Dreams.
  36.  
  37.  */
  38.  
  39. #include "includes.h"
  40. #include <pascal.h>
  41. #include "uucp.h"
  42. #include <Devices.h>
  43. #include <Serial.h>
  44. #include <Errors.h>
  45. #include <LoMem.h>
  46. #include <Desk.h>
  47. #include <Devices.h>
  48.  
  49. #define    CONTROL    "gnuucp.ctl"
  50.  
  51. long total_data_processed = 0;
  52.  
  53. int abort_key_pressed(void);
  54. void HandleEvent (EventRecord *, int);
  55. int gnuucp_cleaned_up = 0;
  56.  
  57.  
  58. /*
  59.  * Exported variables
  60.  */
  61. char     sysdep_control[] = CONTROL;
  62.  
  63. /*
  64.  * Open the serial line for an incoming call.
  65.  * Argument of NULL or empty string means stdin.
  66.  * FIXME, baud rate should be settable here?
  67.  */
  68.  
  69. /* Should be related to MAX packet size we expect */
  70. #define SER_BUF_SIZE 4096
  71. char inbuf[SER_BUF_SIZE];
  72. char outbuf[SER_BUF_SIZE];
  73.  
  74. Boolean GNUPortOpen = FALSE;
  75. int inRefNum;
  76. unsigned char *inRefName;
  77. int outRefNum;
  78. unsigned char *outRefName;
  79.  
  80. void dtr_state(Boolean);
  81.  
  82. void drop_dtr_for (int amt);
  83.  
  84. int
  85. openline(ttynam, baud)
  86.     char    *ttynam;
  87.     int    baud;
  88. {
  89.     int baud_set;
  90.     int compos;
  91.     int err;
  92.     SerShk SerFlags;
  93.     compos = strcmp(".B", ttynam);
  94.     if (compos == -1) 
  95.         {
  96.             inRefNum = binRefNum;
  97.             inRefName = "\p.Bin";
  98.             outRefNum = boutRefNum;
  99.             outRefName = "\p.Bout";
  100.             }
  101.         else
  102.         {
  103.             compos = strcmp(".A", ttynam);
  104.             if (compos == -1)
  105.                 {
  106.                     inRefNum = ainRefNum;
  107.                     inRefName = "\p.Ain";
  108.                     outRefNum = aoutRefNum;
  109.                     outRefName = "\p.Aout";
  110.                     }
  111.             else
  112.                 {
  113.                     logit("Unknown port.", ttynam);
  114.                     sigint(0);
  115.                     }
  116.             }
  117.     switch (baud)
  118.         {
  119.             case 300:
  120.                 baud_set = baud300;
  121.                 break;
  122.             case 1200:
  123.                 baud_set = baud1200;
  124.                 break;
  125.             case 2400:
  126.                 baud_set = baud2400;
  127.                 break;
  128.             case 3600:
  129.                 baud_set = baud3600;
  130.                 break;
  131.             case 4800:
  132.                 baud_set = baud4800;
  133.                 break;
  134.             case 7200:
  135.                 baud_set = baud7200;
  136.                 break;
  137.             case 9600:
  138.                 baud_set = baud9600;
  139.                 break;
  140.             case 19200:
  141.                 baud_set = baud19200;
  142.                 break;
  143.             case 57600:
  144.                 baud_set = baud57600;
  145.                 break;
  146.             default:
  147.                 return(EOF);
  148.                 break;
  149.                 }
  150.     if (OpenDriver(inRefName, (short *)&inRefNum) != noErr)
  151.         return(EOF);
  152.     if (OpenDriver(outRefName, (short *)&outRefNum) != noErr)
  153.         return(EOF);
  154.     GNUPortOpen = TRUE;
  155.     SerReset(inRefNum, baud_set+data8+stop10);
  156.     SerReset(outRefNum, baud_set+data8+stop10);
  157.     SerSetBuf(inRefNum, inbuf, SER_BUF_SIZE);
  158.     SerSetBuf(outRefNum, outbuf, SER_BUF_SIZE);
  159.     SerFlags.fXOn = 0;
  160.     SerFlags.fCTS = 0;
  161.     SerFlags.xOn = 0; 
  162.     SerFlags.xOff = 0;
  163.     SerFlags.errs = 0;
  164.     SerFlags.evts = 0;
  165.     SerFlags.fInX = 0;
  166.     SerFlags.fDTR = 0;
  167.     SerHShake(inRefNum, &SerFlags);
  168.     SerHShake(outRefNum, &SerFlags);    
  169.     dtr_state(TRUE);
  170.     gnuucp_cleaned_up = 0;
  171.     gnusleep(1); /* Let the line settle */ 
  172.     return(0);
  173. }
  174.  
  175. /*
  176.  * Open the serial line for an outgoing call.
  177.  */
  178. int
  179. openout(p)
  180.     struct port *p;
  181.     {
  182.     int tty;
  183.     tty = openline(p->devname, p->baud);
  184.     return(tty);
  185. }
  186.  
  187. int
  188. openin(p)
  189.     struct port *p;
  190.     {
  191.     int tty;
  192.     char *ttynam;
  193.     char *modemname;
  194.     int baud;
  195.     ttynam = p->devname;
  196.     modemname = p->modemname;
  197.     baud = p->baud;
  198.     tty = openline(ttynam, baud);
  199.     if (strcmp(modemname, "none") != 0)
  200.         {
  201.             xwrite(/* 0, */"+++", 3);
  202.             gnusleep(4);
  203.             xwrite(/* 0, */"ats0=2\r", 7);
  204.             }
  205.     return(tty);
  206. }
  207. /*
  208.  * Basement level I/O routines
  209.  *
  210.  * xwrite() writes a character string to the serial port
  211.  * xgetc() returns a character from the serial port, or an EOF for timeout.
  212.  * sigint() restores the state of the serial port on exit.
  213.  * hangup() hangs up the serial port (e.g. drop DTR).
  214.  */
  215.  
  216. void
  217. sigint(val)
  218. int val;
  219. {
  220.     exit(1);
  221.     }
  222.  
  223. xwrite(/* dummy, */ buf, ctr)
  224. /* int dummy; */
  225. char *buf;
  226. int ctr;
  227. {
  228.     long count;
  229.     
  230.     count = ctr;
  231.     
  232.     HandleEvents();
  233.     if (FSWrite(outRefNum, &count, buf) != noErr)
  234.         return(EOF);
  235.     total_data_processed += ctr;
  236.     return((int)count);
  237. }
  238.  
  239. long xgetc_buf_count
  240. unsigned char xgetc_buf[
  241. xgetc()
  242. {
  243.     unsigned char data;
  244.     long count;
  245.     int delay;
  246.     long i;
  247.     
  248.     i = TickCount() + (ByteTimeout * 60L);
  249.     count = 0;
  250.     delay = 500;
  251.     while ((TickCount() < i)) 
  252.     {
  253.         SerGetBuf(inRefNum, &count);
  254.         HandleEvents();
  255.         if (count > 0) 
  256.             {
  257.                 count = 1;    /* make sure we only read one byte */
  258.                 if (FSRead(inRefNum, &count, &data) != noErr)
  259.                     return(EOF);
  260.                    else
  261.                     total_data_processed++;
  262.                     return(data & 0xFF);
  263.                   }
  264.                     else
  265.                    {
  266.                         if (delay == 0)
  267.                             {
  268.                                 gnusleep(1);
  269.                                 delay = 500;
  270.                                 }
  271.                             else
  272.                                 {
  273.                                     delay--;
  274.                                     }
  275.                         }
  276.         }
  277.     return(EOF);
  278. }
  279.  
  280. void
  281. send_break ()
  282. {
  283.     SerSetBrk(outRefNum);
  284.     gnusleep(1);
  285.     SerClrBrk(outRefNum);
  286.     }
  287.     
  288. /*
  289.  * hangup(): hang up the 'fone.
  290.  */
  291. int
  292. hangup(p)
  293. struct port *p;
  294. {
  295.  
  296.     char *modemname;
  297.     modemname = p ->modemname;
  298.     if (GNUPortOpen == TRUE);
  299.         {
  300.             drop_dtr_for (1);
  301.             }
  302.     if (strcmp(modemname, "none") != 0)
  303.         {
  304.             xwrite(/* (int)NULL, */"+++", 3);
  305.             gnusleep(3);
  306.             xwrite(/* (int)NULL, */"ATH\r", 4);
  307.             gnusleep(1);
  308.             xwrite(/* (int)NULL, */"ats0=0\r", 7);
  309.             }
  310.     if (GNUPortOpen == TRUE);
  311.         {
  312.             CloseDriver(inRefNum);
  313.             CloseDriver(outRefNum);
  314.             GNUPortOpen = FALSE;
  315.             }
  316.     gnuucp_cleaned_up = 1;
  317.     }
  318.         
  319.  
  320.  
  321. /*
  322.  * Create a temporary file name for receiving a file into.
  323.  * "name" is the name we will actually eventually want to use for the file.
  324.  * We currently ignore it, but some OS's that can't move files around
  325.  * easily might want to e.g. put the temp file into the same directory
  326.  * that this file is going into.
  327.  *
  328.  * FIXME:
  329.  * This interface should be able to return a "possible" filename, and
  330.  * be re-called if the name is already in use, to get another.
  331.  * This avoids checking here whether the name is good -- saving system calls.
  332.  */
  333. #define MAXTRIES 20
  334. char *
  335. temp_filename(name)
  336.     register char *name;
  337. {
  338.     static char tname[NAMESIZE];
  339.     int i;
  340.     FILE *fd;
  341.     i = 0;
  342.     if (ourpid == 0)
  343.         ourpid = getpid();
  344.     while (TRUE)
  345.         {
  346.             i++;
  347.             (void) sprintf(tname, "TM.u%d", rand());
  348.             if (access(tname, 0) != 0)
  349.                 break;
  350.             if (i == MAXTRIES) 
  351.                 {
  352.                     printf("can't generate unique file name\n");
  353.                     exit(EXIT_ERR);
  354.                     }
  355.     }
  356.     DEBUG(7, "Using temp file %s\n", tname);
  357.     return tname;
  358. }
  359.  
  360.  
  361. /*
  362.  * Transform a filename from a uucp packet (in Unix format) into a local
  363.  * filename that will work in the local file system.
  364.  *
  365.  * This is a real hack; it puts all the files into one big directory.
  366.  * Everything.  Yuk.
  367.  */
  368. char *
  369. munge_filename(name)
  370.     register char *name;
  371. {
  372.     register char *p;
  373.     static char buffer[NAMESIZE+SLOP];
  374.     int len, hostlen;
  375.     int i, j;
  376.     Boolean found_dot = FALSE;
  377.     DEBUG(7, "Munge_filename  input: %s\n", name);
  378.  
  379.     for (p = name + strlen(name); p != name && *(p-1) != '/'; p--) ;
  380.     len = strlen(p);
  381.     for (i = 0, j = 0; i < len; i++, j++)
  382.         {
  383.             if (name[i] == '.') found_dot = TRUE;
  384.             if ((isupper(name[i]) != 0) && (found_dot == TRUE))
  385.                 {
  386.                     buffer[j] = '_';
  387.                     j++;
  388.                     }
  389.             buffer[j] = name[i];
  390.             }
  391.     buffer[j] = '\0';
  392.     DEBUG(7, "Munge_filename output: %s\n", buffer);
  393.     return buffer;
  394. }
  395.  
  396. char *
  397. unmunge_filename(name)
  398.     register char *name;
  399. {
  400.     register char *p;
  401.     static char unbuffer[NAMESIZE+SLOP];
  402.     int len, hostlen;
  403.     int i, j;
  404.     Boolean found_dot = FALSE;
  405.     DEBUG(7, "Unmunge_filename  input: %s\n", name);
  406.  
  407.     for (p = name + strlen(name); p != name && *(p-1) != '/'; p--) ;
  408.     len = strlen(p);
  409.     for (i = 0, j = 0; i < len; i++, j++)
  410.         {
  411.             if (name[i] == '.') found_dot = TRUE;
  412.             if ((name[i] == '_') && (found_dot == TRUE))
  413.                 {
  414.                     i++;
  415.                     unbuffer[j] = toupper(name[i]);
  416.                     }
  417.             unbuffer[j] = name[i];
  418.             }
  419.     unbuffer[j] = '\0';
  420.     DEBUG(7, "Unmunge_filename output: %s\n", unbuffer);
  421.     return unbuffer;
  422. }
  423. /*
  424.  * Uucp work queue scan.
  425.  *
  426.  * gotsome = work_scan(hostname, type);
  427.  * workfile = work_next();
  428.  * void work_done();
  429.  */
  430.  
  431. /*
  432.  * Local variables of the work queue scanner -- not visible to outsiders
  433.  *
  434.  * Workhost and worktype are always null-terminated; keeping their lengths
  435.  * is just a performance hack.  Workprefix is NOT null terminated; various
  436.  * people copy things after it and use the whole string.  Workprefix is
  437.  * exactly workplen long; to append something, strcpy(workprefix+workplen, ...)
  438.  */
  439. #define MAX_TYPE    10
  440. static struct DIR    *workdir = (struct DIR *)NULL;
  441. static struct dirent    *workfile;
  442. static char    workhost[MAX_HOST+1];
  443. static int    workhlen;
  444. static char    worktype[MAX_TYPE+1];
  445. static int    worktlen;
  446. static char    workfirst = 0;
  447. /* FIXME, at the moment this can be local to work_scan() */
  448. static char    workprefix[MAX_TYPE+1+MAX_HOST+1+MAX_TYPE+1+MAX_HOST+6+SLOP];
  449.         /*       D        . hoptoad  / D        . hoptoad  N1234\0 */
  450. static int    workplen;
  451.  
  452.  
  453. void
  454. work_done()
  455. {
  456.     if (workdir)
  457.         closedir(workdir);
  458.     workdir = (struct DIR *) NULL;
  459.     workfile = (struct dirent *) NULL;
  460.     workfirst = 0;
  461. }
  462.  
  463. char *
  464. index(str, ch)
  465. char *str;
  466. char ch;
  467. {
  468.     strchr(str, ch);
  469.     }
  470.     
  471. int
  472. work_scan(host, type)
  473.     char *host;
  474.     char *type;
  475. {
  476.     char *p;
  477.  
  478.     if (!host)
  479.         host = "";
  480.  
  481.     /* If called twice in a row, don't thrash the disk again */
  482.     /* if (strcmp(workhost, host) == SAME &&
  483.         strcmp(worktype, type) == SAME && workfile)
  484.         return 1; */
  485.  
  486.     if (workdir) work_done();        /* Clean up prev call */
  487.  
  488.     workfirst = 1;                /* Initialize for work_next */
  489.     strcpy(workhost, munge_filename(host));
  490.     workhlen = strlen(workhost);
  491.     if (workhlen > sizeof (workhost) -1) 
  492.         {
  493.             printf("WORK_SCAN SIZE RESTRICTION", "workhlen > sizeof(workhost) - 1");
  494.             exit(1);
  495.             }
  496.     if (workhlen > 30) workhlen = 30;        /* Unix uucp limit */
  497.     strcpy(worktype, type);
  498.     worktlen = strlen(worktype);
  499.     if (worktlen > sizeof (worktype) -1) 
  500.         {
  501.             printf("WORK_SCAN SIZE RESTRICTION", "worktlen > sizeof(worktype) - 1");
  502.             exit(1);
  503.             }
  504.     /* Figure out which subdirectory this class of files is in. */
  505.     /* FIXME: doesn't handle "all D. files" since D.myname is separate. */
  506.     sprintf(workprefix, "%s.%s", worktype, workhost);
  507.     /* p = munge_filename(workprefix);
  508.     strcpy(workprefix, p); */
  509.     p = index(workprefix, ':');
  510.     if (p)
  511.         workplen = 1 + p - workprefix;    /* Prefix ends after '/' */
  512.     else
  513.         workplen = 0;        /* No / in munged; hence no dir */
  514.     workprefix[workplen] = '\0';
  515.     DEBUG(7, "Work prefix is =%s=\n", workprefix);
  516.  
  517.     strcpy(workprefix+workplen, ".");
  518.     workdir = opendir(workprefix);    /* Open whatever directory */
  519.     if (workdir == NULL) {
  520.         DEBUG(0, "Can't open queue directory %s\n", workprefix);
  521.         return 0;            /* No work */
  522.     }
  523.  
  524.     return work_look();
  525. }
  526.  
  527.  
  528. static int
  529. work_look()
  530. {
  531.     int len;
  532.     closedir(workdir);
  533.     opendir(workprefix);
  534.     for (;;) {
  535.         workfile = readdir(workdir);
  536.         if (workfile == NULL) {
  537.             DEBUG(7, "work_look readdir null\n", 0);
  538.             work_done();
  539.             return 0;        /* No work */
  540.         }
  541.         DEBUG(7, "work_look readdir %s\n", workfile->d_name);
  542.  
  543.         /* Is it the right type? */
  544.         if (strncmpic(workfile->d_name, worktype, worktlen) == SAME
  545.             && workfile->d_name[worktlen] == '.') {
  546.             /* see if it matches the hostname */
  547.             len = strlen(workfile->d_name);
  548.             /*         "C"        "." "hoptoad" "Xnnnn" */
  549.             if (workhlen == 0 || 
  550.                 (len == worktlen + 1 + workhlen + 5 &&
  551.                  !strncmp(workhost, workfile->d_name+2, workhlen))
  552.                ) {
  553.                 /* Found an entry! */
  554.                 /* FIXME, check grade letter! */
  555.                 DEBUG(7, "work_look found it!\n", 0);
  556.                 return 1;    /* Found work */
  557.             }
  558.         }
  559.     }
  560.     /* NOTREACHED */
  561. }
  562.  
  563.  
  564. char *
  565. work_next()
  566. {
  567.  
  568.     if (!workfirst) {
  569.         if (!workfile || !work_look())
  570.             return (char *)NULL;
  571.     }
  572.     workfirst = 0;
  573.     return unmunge_filename(workfile->d_name);
  574. }
  575.  
  576. /*
  577.  * Routine to return a string that gives the current date and time, and
  578.  * identifies the current process, if on a multiprocess system.
  579.  */
  580. char *
  581. time_and_pid()
  582. {
  583.     unsigned long clock;
  584.     struct tm *tm;
  585.     static int ourpid = 0;
  586.     static char format[] = "%d/%d-%d:%02d:%02d-%d";
  587.     static char outbuf[sizeof(format)];
  588.  
  589.     (void) time(&clock);
  590.     tm = localtime(&clock);
  591.     if (ourpid == 0)
  592.         ourpid = getpid();
  593.     sprintf(outbuf, format,
  594.         tm->tm_mon+1, tm->tm_mday,
  595.         tm->tm_hour, tm->tm_min, tm->tm_sec,
  596.         ourpid);
  597.     return outbuf;
  598. }
  599.  
  600. int
  601. chdir(ndir)
  602. char *ndir;
  603. {
  604.     WDPBRec    pb;
  605.     int err = 0;
  606.     int vol;
  607.     vol = 0;
  608.     CtoPstr(ndir);
  609.     pb.ioCompletion = 0;
  610.     pb.ioNamePtr = (StringPtr)ndir;
  611.     pb.ioVRefNum = 0;
  612.     pb.ioWDDirID = 0;
  613.     /* err = PBHSetVol(&pb,0); */
  614.     PtoCstr((unsigned char *)ndir);
  615.     return(err);
  616.     }
  617.  
  618. #ifdef IGNORED
  619. int
  620. execlp (pgm, arg1, arg2)
  621. char *pgm;
  622. char *arg1;
  623. int arg2;
  624. {
  625.     printf("Going to exec", pgm);
  626.     }
  627. #endif
  628.  
  629. HFileParam Opendir;
  630. char file_name[256];
  631.  
  632. struct DIR*
  633. opendir(path)
  634. char *path;
  635. {
  636.     char tmp_str[256];
  637.     WDPBRec    pb;
  638.     int err = 0;
  639.     int vol;
  640.     vol = 0;
  641.     strcpy(tmp_str, Spool);
  642.     CtoPstr(tmp_str);
  643.     pb.ioCompletion = 0;
  644.     pb.ioNamePtr = (StringPtr)tmp_str;
  645.     pb.ioVRefNum = 0;
  646.     pb.ioWDDirID = 0;
  647.     err = PBOpenWD(&pb,0);
  648.     if (err != noErr)
  649.         return((struct DIR *)0);
  650.     Opendir.ioCompletion = 0;
  651.     Opendir.ioNamePtr = (StringPtr)&file_name;
  652.     Opendir.ioVRefNum = pb.ioVRefNum;
  653.     Opendir.ioFDirIndex = 0;
  654.     Opendir.ioDirID = 0;
  655.     return((struct DIR *)&Opendir);
  656.     }
  657.  
  658. int closedir(dir)
  659. struct DIR *dir;
  660. {
  661.     WDPBRec pb;
  662.     int err;
  663.     pb.ioCompletion = 0;
  664.     pb.ioVRefNum = Opendir.ioVRefNum;
  665.     err = PBCloseWD(&pb, 0);
  666.     return(err);
  667.     }
  668.     
  669. struct dirent current_file;
  670.  
  671. struct dirent*
  672. readdir(dir)
  673. struct DIR *dir;
  674. {
  675.     int err;
  676.     Opendir.ioCompletion = 0;
  677.     Opendir.ioNamePtr = (StringPtr)&file_name;
  678.     Opendir.ioDirID = 0;
  679.     Opendir.ioFDirIndex++;
  680.     err = PBHGetFInfo((HParmBlkPtr)&Opendir, 0);
  681.     if (err != noErr)
  682.         return((struct dirent *)0);
  683.     current_file.d_name = (char *)Opendir.ioNamePtr;
  684.     PtoCstr((unsigned char *)current_file.d_name);
  685.     return(¤t_file);
  686.     }
  687.  
  688. #ifdef IGNORED
  689. char *strtok(in, look_for)
  690.     char *in;
  691.     char *look_for;
  692.     {
  693.         static char *saved;
  694.         char *tmp, *tmp1;
  695.         if (in != NULL)
  696.             {
  697.                 saved = in;
  698.                 }
  699.         tmp = strpbrk(saved, look_for);
  700.         tmp1 = saved;
  701.         saved = tmp + strspn(tmp, look_for);
  702.         *tmp = '\0';
  703.         return(tmp1);
  704.         }
  705. #endif
  706.  
  707. int strncmpic (s1, s2, n)
  708. register char *s1;
  709. register char *s2;
  710. int n;
  711. {
  712.     register int i;
  713.     
  714.     for (i = 0; i < n; i++)
  715.         {
  716.             if (toupper(s1[i]) != toupper(s2[i]))
  717.                 {
  718.                     return(1);
  719.                     }
  720.         }
  721.     return(0);
  722.     }
  723.     
  724. void
  725. bzero(ptr, bytes)
  726. char *ptr;
  727. unsigned bytes;
  728. {
  729.     memset(ptr, 0, bytes);
  730.     }
  731.  
  732. void
  733. bcopy(to, from, count)
  734. char *to, *from;
  735. int count;
  736. {
  737.     memcpy(from, to, count);
  738.     }
  739.  
  740. #ifdef IGNORED
  741. int system(exe)
  742.     char *exe;
  743.     {
  744.         printf("SYSTEM NOT IMPLEMENTED", exe);
  745.         exit(1);
  746.         }
  747. #endif
  748.         
  749. int
  750. access(file, code)
  751.     char *file;
  752.     int code;
  753.     {
  754.         FILE *exists;
  755.         exists = fopen(file, "r");
  756.         if (exists != 0) 
  757.             {
  758.                 fclose(exists);
  759.                 return(0);
  760.                 }
  761.             else
  762.                 {
  763.                     if (errno == bdNamErr)
  764.                             return(0);
  765.                         else
  766.                             return(1);
  767.                     }
  768.                 }
  769.  
  770.     
  771. /* get a random number for names and other stuff */
  772. random()
  773. {
  774.     int it;
  775.     long tmp;
  776.  
  777.     it = time((unsigned long *)&tmp);
  778.     it &= 0xff;
  779.     return (it);
  780. }
  781.  
  782. /* return the index of c in st. The first char is at 0 */
  783. mindex(st, c)
  784. char c, *st;
  785.  
  786. {
  787.     int i;
  788.  
  789.     for (i = 0; st[i] != '\0'; i++) {
  790.     if (st[i] == c)
  791.         return (i);
  792.     }
  793.     return (-1);
  794. }
  795.  
  796. void
  797. gnuucp_cleanup()
  798.     {
  799.         /* hanup the phone This is abnormal so always do the hayes thing */
  800.         if (!gnuucp_cleaned_up)
  801.             {
  802.              if (GNUPortOpen == TRUE)
  803.                 {
  804.                     drop_dtr_for (1);
  805.                 }
  806.                 gnusleep(1);
  807.                 xwrite(/* (int)NULL, */"+++", 3);
  808.                 gnusleep(3);
  809.                 xwrite(/* (int)NULL, */"ATH\r", 4);
  810.                 xwrite(/* (int)NULL, */"+++", 3);
  811.                 gnusleep(1);
  812.                 xwrite(/* (int)NULL, */"ats0=0\r", 7);
  813.             if (GNUPortOpen == TRUE)
  814.                 {
  815.                     CloseDriver(inRefNum);
  816.                     CloseDriver(outRefNum);
  817.                     GNUPortOpen = FALSE;
  818.                 }
  819.             gnuucp_cleaned_up = 1;
  820.         }
  821.     }
  822.  
  823. long eventcounter = 0;
  824. #define EVENTMAX 30
  825.  
  826. void ProcessEvent (void);
  827.  
  828. /* swallow key events looking for Command-. */
  829. void HandleEvents()
  830.     {
  831.         EventRecord event;
  832.         int mine;
  833.         if (eventcounter <= 0)
  834.             {
  835.                 if (abort_key_pressed() == 1) sigint(0);
  836.                 if (WNEIsImplemented())
  837.                 {
  838.                     mine = WaitNextEvent( everyEvent, &event, 0L, 0L);
  839.                        }
  840.                    else
  841.                 {
  842.                     mine = GetNextEvent(everyEvent, &event);
  843.                     }
  844.             HandleEvent(&event, mine);
  845.             eventcounter = EVENTMAX;
  846.             }
  847.             else
  848.                 eventcounter--;
  849.             }
  850.  
  851.  
  852. char *currtime()
  853. {
  854.     static char timebuffer[26];
  855.     register char *tp;
  856.     unsigned long Time;
  857.     char Year[10];
  858.     DateTimeRec MacTimeRec;
  859.     register Intl1Hndl myHndl;
  860.     register int i;    
  861.         GetDateTime(&Time);
  862.         Secs2Date(Time,&MacTimeRec);
  863.     
  864.         myHndl = (Intl1Hndl)IUGetIntl(1);
  865.         tp = timebuffer;
  866.     
  867.         for (i=1; i<4; i++)
  868.             *tp++ = (*myHndl)->days[MacTimeRec.dayOfWeek-1][i]; /* make day of week */
  869.         *tp++ = ',';
  870.         *tp++ = ' ';
  871.         sprintf(tp,"%02d ",MacTimeRec.day); /* now put in day of month */
  872.         tp += 3;
  873.         for (i=1; i<4; i++)
  874.             *tp++ = (*myHndl)->months[MacTimeRec.month-1][i]; /* make month */
  875.         *tp++ = ' ';
  876.         Year[0] = '\0';
  877.         sprintf(Year, "%d", MacTimeRec.year);
  878.         /* Prepare for new century, i used to start at 2 */
  879.         for (i=0; i<4; i++)
  880.             *tp++ = Year[i]; /* make year */
  881.         *tp++ = ' ';
  882.         sprintf(tp,"%02d:%02d:%02d\n",MacTimeRec.hour,MacTimeRec.minute,MacTimeRec.second);
  883.         return(timebuffer);
  884.         }
  885.  
  886. void
  887. gnusleep (time)
  888. unsigned time;
  889. {
  890.     long start_t;
  891.     long curr_t;
  892.     EventRecord theEvent;
  893.     int mine;
  894.     start_t = clock() / CLOCKS_PER_SEC;
  895.     for (curr_t = start_t; 
  896.          curr_t - start_t < time;
  897.          curr_t = clock() / CLOCKS_PER_SEC)
  898.         {
  899.             if (WNEIsImplemented())
  900.                 {
  901.                     mine = WaitNextEvent( everyEvent, &theEvent, 
  902.                        (time - (curr_t - start_t)) * CLOCKS_PER_SEC, 0L);
  903.                        HandleEvent(&theEvent, mine);
  904.                        }
  905.                    else
  906.                     {
  907.                         long finalTime;
  908.                         Delay(CLOCKS_PER_SEC*time, &finalTime);
  909.                         }
  910.                }
  911.     }
  912.  
  913. extern int interrupted;
  914. int abort_key_pressed ()
  915. {
  916.     if (interrupted) 
  917.         {
  918.             interrupted = 0;
  919.             logit("Aborting Mac/gnuucp", "Goodbye");
  920.             return(1);
  921.             }
  922.     return(0);
  923.     }
  924.  
  925. #define hiword(x)        (((short *) &(x))[0])
  926. #define loword(x)        (((short *) &(x))[1])
  927. extern MenuHandle appleMenu;
  928.  
  929. void HandleEvent(event, mine)
  930. EventRecord *event;
  931. int mine;
  932. {
  933.     int key;
  934.     WindowPeek wp;
  935.     long choice;
  936.     Str255 buf;
  937.     
  938.         /*  check for an event  */
  939.         
  940.     SEvtEnb = false;
  941.     SystemTask();
  942.     if (mine != 0) {
  943.         if (!SystemEvent(event))
  944.             goto doEvent;
  945.     }
  946.     else if (event->what == nullEvent) {
  947.         if (FrontWindow() == 0)
  948.             InitCursor();
  949.     }
  950.     return;
  951.     
  952.         /*  handle event  */
  953.  
  954. doEvent:
  955.     if (event->what == mouseDown) {
  956.         switch (FindWindow(event->where, (WindowPtr *)&wp)) {
  957.             case inMenuBar:
  958.                 InitCursor();
  959.                 choice = MenuSelect(event->where);
  960.                 goto doMenu;
  961.             case inSysWindow:
  962.                 SystemClick(event, (WindowPtr)wp);
  963.                 break;
  964.         }
  965.     }
  966.     return;
  967.  
  968.         /*  handle menu choice  */
  969.  
  970. doMenu:    
  971.     switch (hiword(choice)) {
  972.         case 1:        /*  Apple  */
  973.             GetItem(appleMenu, loword(choice), buf);
  974.             OpenDeskAcc(buf);
  975.             break;
  976.         case 2:        /*  File  */
  977.             console_options.pause_atexit = 0;
  978.             exit(0);
  979.             /* no return */
  980.         case 3:        /*  Edit  */
  981.             SystemEdit(loword(choice) - 1);
  982.             break;
  983.     }
  984.     HiliteMenu(0);
  985. }
  986.  
  987.                                     /** Define trap numbers **/
  988. #ifndef _Unimplemented
  989. #define _Unimplemented    0xA89F            /* Unimplemented trap                */
  990. #endif
  991.  
  992. #ifndef _WaitNextEvent
  993. #define _WaitNextEvent    0xA860            /* WaitNextEvent trap                */
  994. #endif
  995.  
  996. #ifndef _OSDispatch
  997. #define _OSDispatch        0xA88F            /* Temporary MF memory calls        */
  998. #endif
  999.  
  1000. /******************************************************************************
  1001.  TrapAvailable
  1002.  
  1003.      Check whether a certain trap exists on this machine. For pre-Mac II
  1004.      machines, trap numbers only go up to 0x1FF.
  1005.  ******************************************************************************/
  1006.  
  1007. Boolean        TrapAvailable(
  1008.     short            trapNum,            /* The trap number to check            */
  1009.     short            tType)                /* OS or Toolbox trap                */
  1010. {
  1011.     SysEnvRec        theWorld;
  1012.     
  1013.     SysEnvirons(1, &theWorld);
  1014.     
  1015.     if ( (tType == ToolTrap)  &&                        /* Toolbox trap        */
  1016.          ((theWorld.machineType < envMachUnknown)  ||    /* 64K ROM machine    */
  1017.          ((theWorld.machineType > envMachUnknown)  &&    /* …512KE, Plus, or    */
  1018.          (theWorld.machineType < envMacII))) ) {        /* …SE                */
  1019.          
  1020.         trapNum &= 0x3FF;                /* Traps numbers are 10 bits long    */
  1021.         
  1022.         if (trapNum > 0x1FF) {            /* Traps only go up to 0x1FF on        */
  1023.             return(false);                /*   these machines                    */
  1024.         }
  1025.     }
  1026.     
  1027.         /* Compare the address of this trap with that of the    */
  1028.         /* unimplemented trap                                    */
  1029.  
  1030.     return( NGetTrapAddress(trapNum, tType)  !=
  1031.             GetTrapAddress(_Unimplemented) );
  1032. }
  1033.  
  1034.  
  1035. /******************************************************************************
  1036.  WNEIsImplemented
  1037.  
  1038.      Check whether the WaitNextEvent is implemented or not
  1039.  ******************************************************************************/
  1040.  
  1041. Boolean        WNEIsImplemented()
  1042. {
  1043.     SysEnvRec    theWorld;                /* System environment                */
  1044.     
  1045.     SysEnvirons(1, &theWorld);            /* Check environment                */
  1046.     
  1047.     if (theWorld.machineType < 0)         /* Old ROMs, definitely not present    */
  1048.         return(FALSE);
  1049.         
  1050.     else                                /* Check for WNE trap                */
  1051.         return(TrapAvailable(_WaitNextEvent, ToolTrap));
  1052. }
  1053.  
  1054. #define NARGS        25
  1055.  
  1056. char *argv[NARGS+1];
  1057.  
  1058. int
  1059. parse(s, t, argvv)
  1060. register char *s, *t;
  1061. char **argvv[];
  1062. {
  1063.     int c, quote = 0, argc = 0;
  1064.         
  1065.     while (c = *s++) {
  1066.         if (c == ' ')
  1067.             continue;
  1068.         if (argc < NARGS)
  1069.             argv[argc++] = t;
  1070.         do {
  1071.             if (c == '\\' && *s)
  1072.                 c = *s++;
  1073.             else if (c == '"' || c == '\'') {
  1074.                 if (!quote) {
  1075.                     quote = c;
  1076.                     continue;
  1077.                 }
  1078.                 if (c == quote) {
  1079.                     quote = 0;
  1080.                     continue;
  1081.                 }
  1082.             }
  1083.             *t++ = c;
  1084.         } while (*s && ((c = *s++) != ' ' || quote));
  1085.         *t++ = 0;
  1086.     }
  1087.     *argvv = argv;
  1088.     return(argc);
  1089. }
  1090.  
  1091.  
  1092. void
  1093. dtr_state (on_or_off)
  1094. Boolean on_or_off;
  1095. {
  1096.     int ControlCode;
  1097.     CntrlParam ParamPtr;
  1098.     if (on_or_off == TRUE) 
  1099.         ControlCode = 17; /* Turn it on */
  1100.     else
  1101.         ControlCode = 18; /* Trun it off */
  1102.     ParamPtr.csCode = ControlCode;
  1103.     Control(outRefNum, ControlCode, &ParamPtr);    
  1104.     }
  1105.  
  1106. void drop_dtr_for (amt)
  1107. int amt;
  1108. {
  1109.     dtr_state(FALSE);
  1110.     gnusleep(amt);
  1111.     dtr_state(TRUE);
  1112.     }
  1113.  
  1114.  
  1115.